import Constants from "../libs/Constants";
import GameData from "../libs/GameData";
import Helper from "../libs/HelperMgr";
import GameManager from "./GameManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class EnemyManager extends cc.Component {

    @property(Array(cc.Prefab))
    enemyList: Array<cc.Prefab> = new Array();

    curIndexI = 0;
    curIndexJ = 0;


    start() {
        if (GameData.instance.gameModel) {
            this.curIndexI = 0;
            this.curIndexJ = 0;
            this.creteEnemyNormal();
            this.schedule(() => {
                this.creteEnemyNormal()
            }, 1)
        } else {
            this.creteEnemyCrazy();
        }

    }

    //正常模式
    creteEnemyNormal() {
        if (GameManager.instace.gameIsStart && !GameManager.instace.gameIsOver) {
            for (let i = this.curIndexI; i < Constants.StageInfo[GameData.instance.gameStage - 1].enemys.length; i++) {
                for (let j = this.curIndexJ; j < Constants.StageInfo[GameData.instance.gameStage - 1].enemys[i].num; j++) {
                    if (this.node.childrenCount < 10) {
                        let prefab: cc.Node = cc.instantiate(this.enemyList[Constants.StageInfo[GameData.instance.gameStage - 1].enemys[i].enemyType]);
                        prefab.setParent(this.node);
                        prefab.setPosition(Helper.randAtoB(0, cc.winSize.width), Helper.randAtoB(0, 300))
                    } else {
                        this.curIndexI = i;
                        this.curIndexJ = j;
                        break;
                    }
                }

            }
        }

    }


    creteEnemyCrazy() {
        if (GameManager.instace.gameIsStart && !GameManager.instace.gameIsOver) {
            for (let i = 0; i < Constants.StageInfo[GameData.instance.gameStage - 1].enemys.length; i++) {
                for (let j = 0; j < Constants.StageInfo[GameData.instance.gameStage - 1].enemys[i].num; j++) {
                    let prefab: cc.Node = cc.instantiate(this.enemyList[Constants.StageInfo[GameData.instance.gameStage - 1].enemys[i].enemyType]);
                    prefab.setParent(this.node);
                    prefab.setPosition(Helper.randAtoB(0, cc.winSize.width), Helper.randAtoB(0, 300))
                }

            }
        }

    }





}
